package org.tools.wstt.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.tools.wstt.reference.ApplicationComponent;
import org.tools.wstt.utils.logger.IWsttLogger;
import org.tools.wstt.utils.logger.WsttLoggerFactory;

/**
 * Servlet Filter implementation class CheckConfigFilter
 */
public class CheckConfigFilter implements Filter, ApplicationComponent {

	private FilterConfig mConfig;
	private ServletContext mContext;
	protected IWsttLogger mLogger;
	
    /**
     * Default constructor. 
     */
    public CheckConfigFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		this.mConfig = null;
		this.mContext = null;
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig pConfig) throws ServletException {
		this.mConfig = pConfig;
		this.mContext = mConfig.getServletContext();
		this.mLogger = WsttLoggerFactory.getLogger(this.getClass());
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest pRequest, ServletResponse pResponse, FilterChain pChain) throws IOException, ServletException {
		HttpServletRequest vReq = (HttpServletRequest) pRequest;
		String vUri = vReq.getRequestURI();
		mLogger.info("CheckConfigFiltering active on " + vUri);
		
		// special pages, let it serve
		if(vUri.indexOf(INTERNAL_PATH_OF_JSPS)  != -1 
				|| vUri.startsWith(URI_TOOLS)
				|| vUri.startsWith(URI_SERVICES)
				|| vUri.startsWith("/resources")
				|| vUri.startsWith("/favicon.ico")) {
			pChain.doFilter(pRequest, pResponse);
		} else if(vUri.equals(vReq.getContextPath()) || vUri.equals("") || vUri.equals("/") || vUri.equals("/index.jsp")) {
			// root pages, redirect
			//RequestDispatcher vReqDispatcher = pRequest.getRequestDispatcher(INTERNAL_PATH_OF_JSPS + URI_TOOLS + "/profile.jsp" );
			//vReqDispatcher.forward(pRequest, pResponse);
			((HttpServletResponse)pResponse).sendRedirect(URI_TOOLS + "/profile");
		} else {
			// proxying
			// if configuration already done
			if(isConfigInitialized(mContext)) {
				// let proxying to destination
				if(!vUri.startsWith(INTERNAL_PATH_FOR_PROXYING)) {
					// adds internal proxy path for proxy servlet activation
					vUri = INTERNAL_PATH_FOR_PROXYING + vUri;
					RequestDispatcher vReqDispatcher = pRequest.getRequestDispatcher(vUri);
					vReqDispatcher.forward(pRequest, pResponse);
				}
				else {
					pChain.doFilter(pRequest, pResponse);
				}
			}
			else {
				// config needs to be done
				RequestDispatcher vReqDispatcher = pRequest.getRequestDispatcher(URI_TOOLS + "proxy_config");
				vReqDispatcher.forward(pRequest, pResponse);
			}
		}
		
		// pass the request along the filter chain
		//pChain.doFilter(pRequest, pResponse);
	}
	
	/**
	 * 
	 * @param pContext
	 * @return
	 */
	public boolean isConfigInitialized(ServletContext pContext) {
		//String vDestPort = (String) pContext.getAttribute(APP_CONFIG_PROXY_DEST_PORT);
		//destPort = (!StringUtils.isEmpty(vDestPort)) ? Integer
		//		.parseInt(vDestPort) : 80;
		return !StringUtils.isEmpty((String) pContext.getAttribute(APP_CONFIG_PROXY_DEST_HOST));
	}

}
